package com.ithui.tidal.websocket.domain.enums;

import lombok.AllArgsConstructor;
import lombok.Getter;

import java.util.Arrays;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * @Author zn5267
 * 请求类型的枚举类
 **/
@Getter
@AllArgsConstructor
public enum WSReqTypeEnum {
    LOGIN(1,"请求登录二维码"),
    HEARTBEAT(2,"心跳包"),
    AUTHORIZE(3,"登录认证"),
    ;
    private final Integer type;
    private final String desc;

    // 缓存, 主要是将类的属性加载到缓存中, 方便根据type值获取对应的枚举对象
    private static Map<Integer, WSReqTypeEnum> cache;

    // 静态代码块, 初始化缓存，具体缓存实现如下
    static {
        cache = Arrays.stream(WSReqTypeEnum.values()).collect(Collectors.toMap(WSReqTypeEnum::getType, Function.identity()));
    }

    public static WSReqTypeEnum of(Integer type) {
        return cache.get(type);
    }

}
